package com.example.KeHuDuan;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.client.SocketClient;

import java.util.ArrayList;
import java.util.List;

import gson.Test.Music;

import static gson.client.InstructionList.DATA_OP.GET_LIST;

public class MusicPlayer extends BaseActivity implements View.OnClickListener, AbsListView.OnScrollListener {
    private static final String TAG = "MusicPlayer";

    private static final int PAGE_SIZE = 20;

    private int mCurrentStartNo = -1;

    private boolean IsPlay = false;

    SocketClient socketClient ;  //SocketClient类

    private List<Music> mList = new ArrayList<>();
    Button playOrPause;
    ListView list_view;
    MusicAdapter myAdapter;
    TextView musicName;
    TextView artist;
    Button back;
    Button lastOne;
    Button nextOne;
    private int currentMusicIndex = -1;

    @Override
    protected  void sendHandlerToService(){
        mServiceBinder.sendHandler(mHandler);
        getMusicListFromService(0 /* first request */);
    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_LIST:
                    mList.addAll((List<Music>) msg.obj);
                    myAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: is done");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        back = (Button) findViewById(R.id.back);
        lastOne = (Button) findViewById(R.id.last_one);
        playOrPause = (Button) findViewById(R.id.play_or_pause);
        nextOne = (Button) findViewById(R.id.next_one);
        musicName = (TextView) findViewById(R.id.music_name);
        artist = (TextView) findViewById(R.id.artist);

        list_view=findViewById(R.id.list_view);
        back.setOnClickListener(this);
        lastOne.setOnClickListener(this);
        playOrPause.setOnClickListener(this);
        nextOne.setOnClickListener(this);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                mServiceBinder.sMusicPlay(position);
                currentMusicIndex = position;
                changeUI(currentMusicIndex);
            }
        });

        initList();

    }

    private void initList() {
        Log.d(TAG, "showList:is create ");
        myAdapter = new MusicAdapter(MusicPlayer.this, mList);
        list_view.setAdapter(myAdapter);
        list_view.setOnScrollListener(this);

    }

    private void changeUI(int index){
        playOrPause.setBackgroundResource(R.drawable.ic_pause);
        musicName.setText(mList.get(index).getMusicName());
        artist.setText(mList.get(index).getArtist());
    }

    public void onClick(View view){
        switch(view.getId()){
            case R.id.back:
                Intent backIntent=new Intent(MusicPlayer.this,MainActivity.class);
                startActivity(backIntent);
                mServiceBinder.stop();
                onBackPressed();
                break;
            case R.id.last_one:
                if (currentMusicIndex == 0) {
                    currentMusicIndex = mList.size() - 1;
                } else {
                    currentMusicIndex--;
                }
                mServiceBinder.sMusicPrevious();
                IsPlay = true;
                changeUI(currentMusicIndex);
                break;
            case R.id.play_or_pause:
                if (IsPlay == false) {
                    mServiceBinder.sMusicContinue();
                    IsPlay = true;
                    playOrPause.setBackgroundResource(R.drawable.ic_pause);
                } else {
                    mServiceBinder.sMusicPause();
                    IsPlay = false;
                    playOrPause.setBackgroundResource(R.drawable.ic_play);
                }

                break;
            case R.id.next_one:
                if (currentMusicIndex == mList.size() - 1) {
                    currentMusicIndex = 0;
                } else {
                    currentMusicIndex++;
                }
                mServiceBinder.sMusicNext();
                IsPlay = true;
                changeUI(currentMusicIndex);
                break;
            default:
                break;
        }
    }

    private void getMusicListFromService(int start_no) {
        if (mServiceBinder != null && start_no > mCurrentStartNo) {
            mCurrentStartNo = start_no;
            mServiceBinder.sGetMusicList(start_no, PAGE_SIZE);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            if (absListView.getLastVisiblePosition() == mList.size() - 1) {
                getMusicListFromService(mList.size());
            }
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
    }
}
